package com.xvaca.spcms.servlet.report;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xvaca.spcms.bean.OrderBean;
import com.xvaca.spcms.dao.ChannelDAO;
import com.xvaca.spcms.dao.OrderDAO;
import com.xvaca.spcms.dao.PipeDAO;

/**
 * Servlet implementation class ReportShowServlet
 */
public class ReportShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 获取当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = sdf.format(new Date());
		
		// 获取查询参数
		String channelId = request.getParameter("channel_id");
		String pipeId = request.getParameter("pipe_id");
		String startAt = currentDate;
		String endAt = currentDate;
		String groupBy = request.getParameter("group"); // 分组方式 day-天，hour-小时
		
		List<OrderBean> list = null;
		if (groupBy != null && !"".equals(groupBy)) {
			
			startAt = request.getParameter("start_at");
			endAt = request.getParameter("end_at");
			
			OrderDAO dao = new OrderDAO();
			list = dao.getOrderGroupCount(startAt, endAt, Integer.parseInt(channelId), Integer.parseInt(pipeId));
		}
		
		request.setAttribute("list", list);
		
		ChannelDAO channelDao = new ChannelDAO();
		PipeDAO pipeDao = new PipeDAO();
		
		request.setAttribute("channels", channelDao.getChannelList());
		request.setAttribute("pipes", pipeDao.getPipeList());
		
		request.setAttribute("start_at", startAt);
		request.setAttribute("end_at", endAt);
		request.setAttribute("channel_id", channelId);
		request.setAttribute("pipe_id", pipeId);
		request.setAttribute("group", groupBy);
		
		request.getRequestDispatcher("report_show.jsp").forward(request, response);
		
	}

}
